//ThreadManager
#pragma once

#pragma warning(push)
#pragma warning(disable:4244)
#include <boost/thread.hpp>
#pragma warning(pop)

#include "DataBus.h"
#include "Bios/User.h"
#include "Register/AMR.h"
#include "Register/BLR.h"
#include "Register/GPR.h"
#include "Register/MCR.h"
#include "Register/VCR.h"
#include "Memory/Memory.h"
#include "Memory/sqlite3x.hpp"

struct VarietyParameter 
{
	char prefix[8];	
	char minorCode[8];
	int multiple;		
	double unitPrice;	
	double majorMargin;	
	double minorMargin;		
	double percentOpenCharge;
	double percentOffsetCharge;			
	double percentOffsetTodayCharge;
	double fixedOpenCharge;
	double fixedOffsetCharge;
	double fixedOffsetTodayCharge;
	Exchange::FLAG exchangeFlag;
};
typedef vector<VarietyParameter> ParameterSet;

class BIOS;
class Bus
{
public:
	Bus(ParameterSet const& parameterSet);
	~Bus();

public:
  	bool CreateBIOS(Exchange::FLAG exchangeFlag, string& errMsg);
	//The externalQueue and connection exist at the same time
	bool IsExchangeAvailable(Exchange::FLAG exchangeFlag) { return (m_chipBusSet[exchangeFlag] && (NULL!=m_biosSet[exchangeFlag])); }

	HANDLE GetQuitHandle() const { return m_hQuit; }
	ContractCodeSet const& GetContractSet() const { return m_contractSet; }
	VectorRegister& GetVectorRegister() { return m_vectorRegister; }
	DealingCache& GetDealingCache() { return m_dealingCache; }
	OrderActionCache& GetOrderActionCache() { return m_actionCache; }
	MarketSnapshot& GetMarketSnapshot() { return m_marketSnapshot; }
	User& GetUser() { return m_user; }
	BIOS* GetBIOS(Exchange::FLAG exchangeFlag) { return m_biosSet[exchangeFlag]; }

	int GetContractIndex(char const* code) const;
	char const* GetContractCode(int index) const;
	void InitExternalBus(Exchange::FLAG exchangeFlag);
	void SubscribeMarket(Exchange::FLAG exchangeFlag, int contractIndex);
	void SubscribeMarket(Exchange::FLAG exchangeFlag, char const* contractCode);

private:
	void CreateVarietys(char const* prefix, Variety& variety);
	void FillVarietyParameter(VarietyParameter const& parameter, Variety& variety);
	void InitContract();
	void InitDatabase();
	void InitThreads();
 	void ReleaseExternalBuses();

private:
	HANDLE m_hQuit;
	ChipBus::ChipBusPtr m_internalQueue;			//businessQueue
	ChipBus::ChipBusPtr m_controlQueue;				//timeoutQueue
	ChipBus::ChipBusPtr m_explicitQueue;			//explicitTimeQueue
	ChipBus::ChipBusPtr m_recordQueue;				//memoryQueue
	ChipBus::ChipBusPtr m_interfaceQueue;			//interfaceQueue
// 	ChipBus::ChipBusPtr m_marketQueue;				//marketQueue
	VectorRegister m_vectorRegister;
	ChipBusSet m_chipBusSet;						//externalQueueSet
	map<Exchange::FLAG, BIOS*> m_biosSet;			//connectionSet

	ContractCodeSet m_contractSet;
	ParameterSet const& m_parameterSet;

	auto_ptr<boost::thread> m_internalThread;		//business service
	auto_ptr<boost::thread> m_controlThread;		//timeout service
	auto_ptr<boost::thread> m_explicitThread;		//explicit service
	auto_ptr<boost::thread> m_memoryThread;			//database service
	auto_ptr<boost::thread> m_ctpExternalThread;	//request service for ctp
	auto_ptr<boost::thread> m_shfeExternalThread;	//request service for shfe
	auto_ptr<boost::thread> m_dceExternalThread;	//request service for dce
	auto_ptr<boost::thread> m_czceExternalThread;	//request service for czce
	auto_ptr<boost::thread> m_cffexExternalThread;	//request service for cffex

	User m_user;
	DealingCache m_dealingCache;
	OrderActionCache m_actionCache;
	MarketSnapshot m_marketSnapshot;
	TimeoutNotifier m_timeoutNotifier;
	sqlite3x::connection m_dealingConnection;
// 	MatcherSet m_matcherSet;
};